//
//  NewArtistViewController.m
//  Mecca
//
//  Created by Nico Villanueva iOS Dev on 26/8/14.
//  Copyright (c) 2014 TapDash Heuristics. All rights reserved.
//

#import "NewArtistViewController.h"

#import "Constant.h"

#import "PostClass.h"

#import "JsonClass.h"

#import "ParserClass.h"

#import "NewArtistTableViewCell.h"

#import "NewArtist.h"

#import "StarRatingView.h"

#import "UIManager.h"

#import "Artist.h"

#import "RotationTabViewController.h"

#import "Album.h"

#import "UserDetails.h"

@interface NewArtistViewController (){
     NSDictionary* json;
}

@end

@implementation NewArtistViewController



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Do any additional setup after loading the view from its nib.
    
    [_tableView setBackgroundColor:[UIColor colorWithRed:26.0/255.0 green:26.0/255.0 blue:26.0/255.0 alpha:1.0]];
    
    
    self.receivedData = [NSMutableData data]; // Data
    NSURLRequest *request = [[PostClass sharedItems] getNewArtist:[UIDevice currentDevice].identifierForVendor.UUIDString];
    _theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    


}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
    NSLog(@"Back Button");
}


#pragma mark - TableView Delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return [[[ParserClass sharedItems] newArtist] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Configure the cell...
    
    static NSString *cellIdentifier = @"Cell";
    
    NewArtistTableViewCell *cell = (NewArtistTableViewCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    
    if (cell == nil) {
        
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"NewArtistTableViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    
    
    
    UIView *selectionColor = [[UIView alloc] init];
    selectionColor.backgroundColor = [UIColor colorWithRed:(38/255.0) green:(38/255.0) blue:(38/255.0) alpha:1];
    cell.selectedBackgroundView = selectionColor;
    

    NewArtist *newArtist = [[ParserClass sharedItems] newArtist][indexPath.row];
    
    int ratings = [[newArtist rating] intValue];
    
    
    cell.lblArtistName.text = [newArtist fullName];
    
    StarRatingView* starViewNoLabel = [[StarRatingView alloc]initWithFrame:CGRectMake(215, 19, 45, 8) andRating:ratings withLabel:NO animated:YES];
    
    [starViewNoLabel setUserInteractionEnabled:NO];
    
//    [cell.customCell addSubview:starViewNoLabel];
    //[cell.lblNumberOfSongs setText:[newArtist song]];
    
    cell.lblNumberOfSongs.text = [NSString stringWithFormat:@"%@ Songs",[newArtist song]];
    [cell setImageString:[NSString stringWithFormat:@"http://192.185.93.102/~boomeran/backend-panel/%@",[newArtist img]]];
    

    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NewArtist *artist = [[[ParserClass sharedItems] newArtist] objectAtIndex:indexPath.row];
    
    [[ParserClass sharedItems] getArtistID:[artist artistID]];
    
    [[ParserClass sharedItems] getArtistName:[artist fullName]];
    
    [[ParserClass sharedItems] getArtistImg:[artist img]];
    
    [self presentViewController:[[UIManager sharedItems] artistTabBars] animated:YES completion:nil];
}


- (void)fetchedData:(NSData *)responseData {
    
    json = [JsonClass jsonParser:responseData];
    
    [[ParserClass sharedItems] getNewArtist:json];
    
    [self.tableView reloadData];
    
    NSLog(@"New artist = %@", json);
    
    NSLog(@"Datas = %i",[[[ParserClass sharedItems] newArtist] count]);
    
}

#pragma mark - URL Connection Delegate

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [_receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"Error data = %@", error);
    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Failed" message:@"Please check you internet connection." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    
    [alertView show];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    
    
    if (connection == self.theConnection) {
        
        [_receivedData appendData:data];
        
        NSLog(@"data parse = %@", _receivedData);
        
    }
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    
    if (connection == self.theConnection) {
        
        if (!(_receivedData == nil)) {
            
            NSError *error;
            
            NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:_receivedData options:kNilOptions error:&error];
            
            NSLog(@"Received Data = %@", jsonData);
            
            [[ParserClass sharedItems] getNewArtist:jsonData];
            
            [self.tableView reloadData];
            
            NSLog(@"New artist = %@", jsonData);
            
            NSLog(@"Datas = %i",[[[ParserClass sharedItems] newArtist] count]);
            
        }
        
        NSLog(@"Finish");
    }
    
}


@end
